<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/payment.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="$('#form').submit();" class="button"><?php echo $button_save; ?></a><a onclick="location = '<?php echo $cancel; ?>';" class="button"><?php echo $button_cancel; ?></a></div>
    </div>
    <div class="content">
      <div id="tabs" class="htabs"><a href="#tab_coupon"><?php echo $tab_coupon; ?></a><a href="#tab_customer"><?php echo $tab_customer; ?></a></div>
      <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form">
        <div id="tab_coupon">
          <table class="form">
            <tr>
              <td><?php echo $text_coupon_search; ?></td>
              <td><input type="text" name="coupon_search" value="" /></td>
            </tr>
            <tr>
              <td><?php echo $entry_name; ?></td>
              <td><input type="hidden" id="coupon_rule_id" name="coupon_rule_id"/><b id="name"></b></td>
            </tr>
            <tr>
              <td><?php echo $entry_discount_type; ?></td>
              <td>
              	<b style="display: none;" id="discount_type_0"><?php echo $text_discount_type_0; ?></b>
                <b style="display: none;" id="discount_type_1"><?php echo $text_discount_type_1; ?></b>
                <b style="display: none;" id="discount_type_2"><?php echo $text_discount_type_2; ?></b>
              </td>
            </tr>
            <tr>
              <td><?php echo $entry_discount_points; ?></td>
              <td><b id="discount_points"></b></td>
            </tr>
            <tr>
              <td><?php echo $entry_discount_values; ?></td>
              <td><b id="discount_values"></b></td>
            </tr>
            <tr>
              <td><?php echo $entry_max_use_time; ?></td>
              <td><b id="max_use_time"></b></td>
            </tr>
            <tr>
              <td><?php echo $entry_day_use; ?></td>
              <td><b id="day_use"></b></td>
            </tr>
            <tr>
              <td><?php echo $entry_category_ids; ?></td>
              <td><b id="category_ids"></b></td>
            </tr>
            <tr>
              <td><?php echo $entry_description; ?></td>
              <td>
        		<span id="description"></span>
        	  </td>
            </tr>
          </table>
        </div>
        <div id="tab_customer">
          <table class="form">
            <tr>
              <td><?php echo $text_customer_search; ?></td>
              <td><input type="text" name="customer_search" value="" /></td>
            </tr>
          </table>
          <table class="list">
            <thead>
              <tr>
                <td class="center"><?php echo $column_customer_name; ?></td>
                <td class="center"><?php echo $column_email; ?></td>
                <td class="center"><?php echo $column_grade_name; ?></td>
                <td></td>
              </tr>
            </thead>
            <tbody id="tbody_customer_search"></tbody>
          </table>
        </div>
      </form>
    </div>
  </div>
</div>
<script type="text/javascript"><!--	
$('input[name=\'coupon_search\']').autocomplete({
	delay: 0,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=sale/coupon_rule/autocompleteCouponRule&token=<?php echo $token; ?>&filter_display_name=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item.display_name,
						value: item.display_name,
						coupon_rule_id: item.coupon_rule_id,
						discount_type: item.discount_type,
						discount_points: item.discount_points,
						discount_values: item.discount_values,
						max_use_time: item.max_use_time,
						max_use_day: item.max_use_day,
						category_ids: item.category_ids,
						description: item.description
					}
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$("#name").text(ui.item.value);
		$("#coupon_rule_id").val(ui.item.coupon_rule_id);
		$discount_type = ui.item.discount_type;
		if($discount_type == 0){
			$("#discount_type_0").show();
		}else if($discount_type == 1){
			$("#discount_type_1").show();
		}else{
			$("#discount_type_2").show();
		}
		$("#discount_points").text(ui.item.discount_points);
		$("#discount_values").text(ui.item.discount_values);
		$("#max_use_time").text(ui.item.max_use_time);
		$("#day_use").text(ui.item.max_use_day);
		$("#category_ids").text(ui.item.category_ids);
        $("#description").text(ui.item.description);
		return false;
	},
	focus: function(event, ui) {
      return false;
    }
});
//--></script>
<script type="text/javascript"><!--	
$customer_rows = 0;
$('input[name=\'customer_search\']').autocomplete({
	delay: 0,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=customer/customer/autocomplete&token=<?php echo $token; ?>&filter_email=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {
				response($.map(json, function(item) {
					return {
						label: item.email,
						value: item.customer_id,
						name: item.name,
						grade_name: item.grade_name
					}
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$html = '<tr>';
		$html += '<td class="left">' + ui.item.firstname + ' ' + ui.item.lastname + '</td>';
		$html += '<td class="left">' + ui.item.label + '</td>';
		$html += '<td class="left">' + ui.item.grade_name + '</td>';
		$html += '<td class="right">';
		$html += '<input name="customer[' + $customer_rows + '][customer_id]" type="hidden" value="' + ui.item.value + '"/>';
		$html += '<a onclick="$(this).parent().parent().remove();" class="button"><?php echo $button_remove; ?></a>';
		$html += '</td>';
		$html += '</tr>';
		$("#tbody_customer_search").append($html);
		$customer_rows++;
		return false;
	},
	focus: function(event, ui) {
      return false;
    }
});
//--></script>
<script type="text/javascript" src="view/javascript/jquery/ui/jquery-ui-timepicker-addon.js"></script> 
<script type="text/javascript"><!--
$('.date').datepicker({dateFormat: 'yy-mm-dd'});
//--></script>
<script type="text/javascript"><!--
$('#tabs a').tabs(); 
//--></script>
<?php echo $footer; ?>